from rest_framework.generics import CreateAPIView, GenericAPIView, ListAPIView, ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from meiduo_admin.serializer.users import AdminAuthSerializer, UserSerializer
from users.models import User


class AdminAuthorizeView(CreateAPIView):
    serializer_class = AdminAuthSerializer


class UserInfoView(ListCreateAPIView):
    permission_classes = [IsAdminUser]
    serializer_class = UserSerializer

    def get_queryset(self):
        # 类师徒中含有request属性
        keyword = self.request.query_params.get('keyword')

        if keyword:
            users = User.objects.filter(is_staff=False, username__contains=keyword)
        else:
            users = User.objects.filter(username__contains=keyword)
        return users

